package me.mononin.dilechat.fragments.account;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.yalantis.ucrop.UCrop;

import net.qiujuer.genius.ui.widget.Loading;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import me.mononin.common.common.app.Application;
import me.mononin.common.common.app.PresenterFragment;
import me.mononin.dilechat.R;
import me.mononin.dilechat.activities.MainActivity;
import me.mononin.dilechat.fragments.media.GalleryFragment;
import me.mononin.factory.preseneter.user.UpdateInfoContract;
import me.mononin.factory.preseneter.user.UpdateInfoPresenter;

import static android.app.Activity.RESULT_OK;

public class AccountInfoFragment extends PresenterFragment<UpdateInfoContract.Presenter>
        implements UpdateInfoContract.View {


    @BindView(R.id.loading)
    Loading mLoading;

    @BindView(R.id.btn_submit)
    Button mSubmit;

    @BindView(R.id.im_sex)
    ImageView mSex;

    @BindView(R.id.edit_desc)
    EditText mDesc;
    @BindView(R.id.account_user_ic)
    CircleImageView accountImage;

    private String mPortraitPath;
    private boolean isMan = true;

    public AccountInfoFragment() {
        // Required empty public constructor
    }


    @Override
    protected int getContentId() {
        return R.layout.fragment_account_info;
    }


    @OnClick({R.id.account_user_ic, R.id.btn_submit, R.id.im_sex})
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.account_user_ic:
                new GalleryFragment()
                        .setListener(new GalleryFragment.OnSelectedListener() {
                            @Override
                            public void onSelected(String path) {
                                UCrop.Options options = new UCrop.Options();
                                //设置图片处理格式JPEG
                                options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
                                //设置图片压缩精度
                                options.setCompressionQuality(96);

                                File dPath = Application.getPortraitTmpFile();

                                UCrop.of(Uri.fromFile(new File(path)), Uri.fromFile(dPath))
                                        .withAspectRatio(1, 1)
                                        .withMaxResultSize(250, 250)
                                        .withOptions(options)
                                        .start(getActivity());
                            }
                        })
                        //show的时候建议使用getChildFragmentManager
                        .show(getChildFragmentManager(), GalleryFragment.class.getName());
                break;
            case R.id.btn_submit:
                String desc = mDesc.getText().toString();
                // 调用P层进行注册
                mPresenter.updateInfo(mPortraitPath, desc, isMan);
                break;
            case R.id.im_sex:
                // 性别图片点击的时候触发
                isMan = !isMan; // 反向性别

                Drawable drawable = getResources().getDrawable(isMan ?
                        R.drawable.ic_sex_man : R.drawable.ic_sex_woman);
                mSex.setImageDrawable(drawable);
                // 设置背景的层级，切换颜色
                mSex.getBackground().setLevel(isMan ? 0 : 1);
                break;
            default:
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
            final Uri resultUri = UCrop.getOutput(data);
            if (resultUri != null) {
                loadPortrait(resultUri);
            }

        } else if (resultCode == UCrop.RESULT_ERROR) {
            Application.showToast(R.string.data_rsp_error_unknown);
        }

    }


    private void loadPortrait(Uri uri) {

        mPortraitPath = uri.getPath();

        Glide.with(this)
                .load(uri)
                .asBitmap()
                .centerCrop()
                .into(accountImage);
    }

    @Override
    protected UpdateInfoContract.Presenter initPresenter() {
        return new UpdateInfoPresenter(this);
    }

    @Override
    public void updateSucceed() {
        MainActivity.show(getContext());
        getActivity().finish();
    }


    @Override
    public void showError(int str) {
        super.showError(str);
        // 当需要显示错误的时候触发，一定是结束了

        // 停止Loading
        mLoading.stop();
        // 让控件可以输入
        mDesc.setEnabled(true);
        accountImage.setEnabled(true);
        mSex.setEnabled(true);
        // 提交按钮可以继续点击
        mSubmit.setEnabled(true);
    }

    @Override
    public void showLoading() {
        super.showLoading();

        // 正在进行时，正在进行注册，界面不可操作
        // 开始Loading
        mLoading.start();
        // 让控件不可以输入
        mDesc.setEnabled(false);
        accountImage.setEnabled(false);
        mSex.setEnabled(false);
        // 提交按钮不可以继续点击
        mSubmit.setEnabled(false);
    }
}
